//
//  MyInfomationViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/8/10.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "MyInfomationViewController.h"
#import "MyInfoCell.h"
#import "MyMessageModel.h"

@interface MyInfomationViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *array;

@end

static NSString *infoCellIdentifier = @"MyInfoCell";

@implementation MyInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"我的消息"];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [self.myTableView registerNib:[UINib nibWithNibName:@"MyInfoCell" bundle:nil]
         forCellReuseIdentifier:infoCellIdentifier];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"tuisong" object:nil];
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.myTableView.header beginRefreshing];}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken};
    [RequestData myMessage:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
            [self.myTableView.header endRefreshing];
        } else {
            [self.myTableView.header endRefreshing];
            self.array = jsonArray;
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:infoCellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MyInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:infoCellIdentifier];
    }
    cell.model = [_array objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:infoCellIdentifier];
    cell.model = [_array objectAtIndex:indexPath.row];
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;
}

@end
